package com.sinosoft.web.easyui.api;

import cn.com.sinux.spring.commons.PropertyCollection;
import cn.com.sinux.spring.json.JsonFilter;
import cn.com.sinux.spring.utils.BeanUtils;
import cn.com.sinux.spring.utils.PageUtils;
import com.sinosoft.hydra.um.condition.SearchDirectoryCondition;
import com.sinosoft.hydra.um.constant.BaseConstant;
import com.sinosoft.hydra.um.domain.model.DirectoryType;
import com.sinosoft.hydra.um.domain.permission.TDirectory;
import com.sinosoft.hydra.um.helper.LoggerHelper;
import com.sinosoft.hydra.um.services.DirectoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Collections;
import java.util.List;

/**
 * 数据字典API
 */
@Controller
@RequestMapping("api/directory")
public class SysDirectoryApi {

    @Autowired
    private DirectoryService directoryService;

    /**
     * 添加字典项
     *
     * @param directory 表单对象
     * @return 保存结果
     * @throws Exception
     */
    @RequestMapping(value = "addDirectory", method = RequestMethod.POST)
    @RequiresPermissions(value = "directory:data:add")
    @ResponseBody
    public Object addDirectory(TDirectory directory) throws Exception {
        directoryService.addDirectory(directory);
        LoggerHelper.logInfo("添加了字典项‘{}’", directory.getDirValue());
        return true;
    }

    /**
     * 修改字典项
     *
     * @param directory 表单对象
     * @return 保存结果
     * @throws Exception
     */
    @RequestMapping(value = "updateDirectory", method = RequestMethod.POST)
    @RequiresPermissions(value = "directory:data:update")
    @ResponseBody
    public Object updateDirectory(TDirectory directory) throws Exception {
        TDirectory db = directoryService.findOne(directory.getId());
        TDirectory source = new TDirectory();
        BeanUtils.copyProperties(db, source);
        TDirectory result = directoryService.updateDirectory(directory);
        LoggerHelper.logInfo("修改了字典项‘{}’{}", db.getDirValue(), new LoggerHelper.PropertyMerge(source, result)
                        .addPropertyDes("dirCode", "字典项Key")
                        .addPropertyDes("dirValue", "字典值")
        );
        return JsonFilter.doPropertyFilter(directory, new PropertyCollection("id"));
    }

    /**
     * 删除字典项
     *
     * @param searchDirectoryCondition 删除集合ids
     * @return 删除结果
     * @throws Exception
     */
    @RequestMapping(value = "deleteDirectories", method = RequestMethod.POST)
    @RequiresPermissions(value = "directory:data:delete")
    @ResponseBody
    public Object deleteDirectories(SearchDirectoryCondition searchDirectoryCondition) throws Exception {
        List<TDirectory> directories = directoryService.deleteDirectories(searchDirectoryCondition.getIds());
        LoggerHelper.logInfo("删除了字典项‘{}’", new LoggerHelper.PropertyReader<TDirectory>(directories, "dirValue"));
        return true;
    }

    /**
     * 搜索字典项
     *
     * @param searchDirectoryCondition 搜索条件
     * @return 搜索结果
     * @throws Exception
     */
    @RequestMapping(value = "findDirectories")
    @RequiresPermissions(value = "directory:data:list")
    @ResponseBody
    public Object updateDirectory(
            @RequestParam(value = "page", required = false, defaultValue = "0") Integer pageNumber,
            @RequestParam(value = "rows", required = false, defaultValue = "10") Integer pageSize,
            SearchDirectoryCondition searchDirectoryCondition) throws Exception {
//        Pageable page = PageUtils.buildPageRequest(pageNumber, pageSize, "id", "desc");
        Pageable page = PageUtils.buildPageRequest(pageNumber, pageSize, "dirOrder", "asc");
        searchDirectoryCondition.setDisplay(BaseConstant.INTEGER_TRUE);
        Page<TDirectory> devicePage = directoryService.findDirectoriesByPage(searchDirectoryCondition, page);
        for (TDirectory directory : devicePage) {
            directory.setDirectoryType(DirectoryType.valueOf(directory.getDirGroup()));
        }
        return JsonFilter.doPropertyFilter(devicePage, new PropertyCollection()
                .addProperties(TDirectory.class, "id", "dirCode", "dirOrder", "dirDescription", "dirValue", "dirGroup", "directoryType"));
    }

    /**
     * 根据类型搜索字典项
     *
     * @param dirGroup 字典组
     * @return 搜索结果
     * @throws Exception
     */
    @RequestMapping(value = "list")
    @ResponseBody
    public Object directories(@RequestParam(value = "dirGroup", required = true) String dirGroup) throws Exception {
        DirectoryType directoryType = DirectoryType.valueOf(dirGroup);
        if (directoryType == null) {
            return Collections.emptyList();
        }
        SearchDirectoryCondition searchDirectoryCondition = new SearchDirectoryCondition();
        searchDirectoryCondition.setDisplay(BaseConstant.INTEGER_TRUE);
        searchDirectoryCondition.setDirGroup(dirGroup);
        Page<TDirectory> devicePage = directoryService.findDirectoriesByPage(searchDirectoryCondition, PageUtils.buildPageRequest(1, 999, "dirOrder", "asc"));
        return JsonFilter.doPropertyFilter(devicePage, new PropertyCollection()
                .addProperties(TDirectory.class, "id", "dirValue"));
    }

    /**
     * 修改字典项页面
     *
     * @return String 页面名称
     */
    @RequestMapping("toUpdate")
    public String moduleDocument(Model model,
                                 @RequestParam(value = "id", required = true) String id) {
        model.addAttribute("directory", directoryService.getOne(id));
        model.addAttribute("directoryTypes", DirectoryType.values());
        return "hydra_easyui/directory/updateDirectory";
    }

    /**
     * 搜索页面
     *
     * @return 页面
     */
    @RequestMapping("toSearch")
    @RequiresPermissions(value = "directory:data:list")
    public String toSearch(Model model) {
        model.addAttribute("directoryTypes", DirectoryType.values());
        return "hydra_easyui/directory/searchDirectory";
    }

    /**
     * 新增字典项页面
     *
     * @return 新增页面
     */
    @RequestMapping("toAdd")
    @RequiresPermissions(value = "directory:data:add")
    public String toAdd(Model model) {
        model.addAttribute("directoryTypes", DirectoryType.values());
        return "hydra_easyui/directory/addDirectory";
    }

    /**
     * 字典项首页面
     *
     * @return 页面
     */
    @RequestMapping("index")
    @RequiresPermissions(value = "directory:data:list")
    public String index() {
//        LoggerHelper.logInfo("访问了字典项列表页");
        return "hydra_easyui/directory/index";
    }
}
